package com.selenium.core.functionlibrary;

import java.util.Arrays;

import org.testng.Reporter;

import com.selenium.core.SelTestCase;

/**
 * Provides Logging for selenium methods.
 * 
 * @author tbhadauria
 */
public class LogSelenium extends SelTestCase {

	/** 
	 * Provides logging for <code>type</code> method of 
	 * <code>SeleneseTestBase</code> class
	 * 
	 * @param textBoxLocator	Text box locator
	 * @param testData	Data to be keyed in	
	 * @param webObject	User Identifier for object Should <b>NOT</b> contain spaces
	 * @param logMessage Boolean flag to determine logging 
	 */
	public static void type(String textBoxLocator, String testData, String webObject, 
			Boolean logMessage) {

		// Type in text box.
		selenium.type(textBoxLocator, testData);

		if (logMessage) {

			// Log the success message if type operation was successful.
			if(testData.equals(selenium.getValue(textBoxLocator))) {
				Reporter.log(new StringBuilder("<span style=\"color:green;\">")
				.append(testData).append(" is entered in ")
				.append(webObject).append("</span> <br>").toString());

			} else {
				Reporter.log(new StringBuilder("<span style=\"background: pink; " +
				"color:blue\">").append("Entered test data doesn't appear in ")
				.append(webObject).append(" Entered value was: ")
				.append(testData).append(" while text box value is: ")
				.append(selenium.getValue(textBoxLocator))
				.append("</span> <br>").toString());					
			}
		}
	}

	/**
	 * Provides logging for <code>check</code> method of 
	 * <code>SeleneseTestBase</code> class
	 * 
	 * @param elementLocator	Check box/Radio button locator
	 * @param webObject	User Identifier for object Should <b>NOT</b> contain spaces	
	 * @param logMessage Boolean flag to determine logging
	 */
	public static void check(String elementLocator, String webObject, Boolean logMessage) {

		// Check the check box or radio button.
		selenium.check(elementLocator);

		if (logMessage) {

			// Log the success message if check operation was successful.
			if(selenium.isChecked(elementLocator)) {
				Reporter.log(new StringBuilder("<span style=\"color:green;\">")
				.append(webObject).append(" is checked/selected ")
				.append("</span> <br>").toString());

			} else {
				Reporter.log(new StringBuilder("<span style=\"background: pink; " +
				"color:blue\">").append(webObject).append(" is NOT checked/selected: ")
				.append("</span> <br>").toString());					
			}			
		}
	}
	
	/**
	 * Provides logging for <code>click</code> method of 
	 * <code>SeleneseTestBase</code> class
	 * 
	 * @param elementLocator	Check box/Radio button locator
	 * @param webObject	User Identifier for object Should <b>NOT</b> contain spaces	
	 * @param logMessage Boolean flag to determine logging
	 */
	public static void click(String elementLocator, String webObject, Boolean logMessage) {

		// Check the check box or radio button.
		selenium.click(elementLocator);

		if (logMessage) {
							
				Reporter.log(new StringBuilder("<span style=\"color:green;\">")
				.append(webObject).append(" is clicked ")
				.append("</span> <br>").toString());
		}
	}
	
	/**
	 * Provides logging for <code>select</code> method of 
	 * <code>SeleneseTestBase</code> class
	 * 
	 * @param dropdownLocator	Drop down locator
	 * @param testData Drop down value to be selected
	 * @param webObject	User Identifier for object Should <b>NOT</b> contain spaces	
	 * @param logMessage Boolean flag to determine logging
	 */
	public static void select(String dropdownLocator, String testData, String webObject, Boolean logMessage) {

		// Check the check box or radio button.
		selenium.select(dropdownLocator, testData);

		if (logMessage) { 

			// Log the success message if selection made is available.
			if(testData.equals("label="+(selenium.getSelectedLabel(dropdownLocator)))) {
				Reporter.log(new StringBuilder("<span style=\"color:green;\">")
				.append(webObject).append(" is selected for value - ")
				.append(testData)
				.append("</span> <br>").toString());
 
			} else {
				Reporter.log(new StringBuilder("<span style=\"background: pink; " +
				"color:blue\">").append(webObject).append(" is NOT selected. " +
						"Drop down selection is: ").
						append(selenium.getSelectedLabel(dropdownLocator))
				.append("</span> <br>").toString());					
			}			
		}
	}
	
	/**
	 * Provides logging for <code>getSelectOptions</code> method of 
	 * <code>SeleneseTestBase</code> class
	 * 
	 * @param elementLocator	Check box/Radio button locator
	 * @param webObject	User Identifier for object Should <b>NOT</b> contain spaces	
	 * @param logMessage Boolean flag to determine logging
	 */
	public static void getSelectOptions(String elementLocator, String webObject, Boolean logMessage) {

		// Check the check box or radio button.
		String[] dropDownvalues = selenium.getSelectOptions(elementLocator);

		if (logMessage) {
		
				Reporter.log(new StringBuilder("<span style=\"color:green;\">")
				.append(webObject).append(" values are: ")
				.append(Arrays.toString(dropDownvalues))
				.append("</span> <br>").toString());
		}
	}
}